#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::string;

void test()
{
    vector<int> vInt;//创建空对象
    vector<string> vStr;
    string s1("wangdao");

    vInt.push_back(1);//在尾部插入元素
    vInt.push_back(2);
    cout << "vInt[0] = " << vInt[0] << endl;//可以使用[]
    cout << "vInt[1] = " << vInt[1] << endl;

    cout << endl << endl;
    vStr.push_back("hello");
    vStr.push_back(s1);
    cout << "vStr[0] = " << vStr[0] << endl;
    cout << "vStr[1] = " << vStr[1] << endl;
}

void test2()
{
    //vector的底层扩容的原理：当元素的个数size()与容量的大小
    //capacity()相等的时候，如果再继续使用push_back插入元素
    //那么就会发生扩容，新的空间的大小就是老的空间的两倍
    //
    //具体过程是：会重新找一块新的空间，新的空间的大小是老的
    //空间的两倍，然后将老的空间上的元素拷贝到新的空间来，
    //在将老空间上的元素销毁以及空间回收
    vector<int> vec;//创建空对象
    cout << "元素的个数  = " << vec.size() << endl;
    cout << "容量的大小  = " << vec.capacity() << endl;

    cout << endl;
    vec.push_back(10);
    cout << "元素的个数  = " << vec.size() << endl;
    cout << "容量的大小  = " << vec.capacity() << endl;

    cout << endl;
    vec.push_back(20);
    cout << "元素的个数  = " << vec.size() << endl;
    cout << "容量的大小  = " << vec.capacity() << endl;

    cout << endl;
    vec.push_back(30);
    cout << "元素的个数  = " << vec.size() << endl;
    cout << "容量的大小  = " << vec.capacity() << endl;

    cout << endl;
    vec.push_back(40);
    cout << "元素的个数  = " << vec.size() << endl;
    cout << "容量的大小  = " << vec.capacity() << endl;

    cout << endl;
    vec.push_back(5);
    cout << "元素的个数  = " << vec.size() << endl;
    cout << "容量的大小  = " << vec.capacity() << endl;
}

int main(int argc, char *argv[])
{
    test2();
    return 0;
}

